package com.atguigu.gmall.list.service.impl;

import com.atguigu.gmall.list.dao.GoodsDao;
import com.atguigu.gmall.list.service.GoodsService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.ProductFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Resource
    private GoodsDao goodsDao;
    @Autowired
    private ProductFeign productFeign;
    /**
     * 通过skuId返回skuInfo 然后将数据库中商品存入es
     *
     * @param skuId
     */
    @Override
    public void dbSkuAddIntoEs(Long skuId) {
        //查询商品信息
        SkuInfo skuInfo = productFeign.getSkuInfo(skuId);
        //判断商品信息是否为空
        if (skuInfo == null || skuInfo.getId() == null){
            //不存在直接返回
            return;
        }
        //存在进行写入es
        //将skuInfo转化为goods对象
        Goods goods = new Goods();
        goods.setId(skuInfo.getId());
        goods.setDefaultImg(skuInfo.getSkuDefaultImg());
        goods.setTitle(skuInfo.getSkuName());
        //价格随时会变化单独查询
        BigDecimal price = productFeign.getPrice(skuId);
        goods.setPrice(price.doubleValue());
        goods.setCreateTime(new Date());
        //查询品牌信息后存入goods 存入es
        BaseTrademark trademark =
                productFeign.getTrademark(skuInfo.getTmId());
        goods.setTmId(trademark.getId());
        goods.setTmName(trademark.getTmName());
        goods.setTmLogoUrl(trademark.getLogoUrl());
        //查询分类信息 存入goods
        BaseCategoryView category =
                productFeign.getCategory(skuInfo.getCategory3Id());
        goods.setCategory1Id(category.getCategory1Id());
        goods.setCategory2Id(category.getCategory2Id());
        goods.setCategory3Id(category.getCategory3Id());
        goods.setCategory1Name(category.getCategory1Name());
        goods.setCategory3Name(category.getCategory3Name());
        goods.setCategory2Name(category.getCategory2Name());
        //获取平台属性
        List<BaseAttrInfo> skuAttrValues =
                productFeign.getSkuAttrValue(skuId);
        //遍历 存入searchAttr
        List<SearchAttr> attrs = skuAttrValues.stream().map(skuAttrValue -> {
            SearchAttr searchAttr = new SearchAttr();
            searchAttr.setAttrId(skuAttrValue.getId());
            searchAttr.setAttrName(skuAttrValue.getAttrName());
            searchAttr.setAttrValue(skuAttrValue.
                    getAttrValueList().get(0).getValueName());
            return searchAttr;
        }).collect(Collectors.toList());
        //存入goods
        goods.setAttrs(attrs);
        //调用内置api 存入es
        goodsDao.save(goods);
    }

    /**
     * 通过商品id删除es中的数据
     *
     * @param goodsId
     */
    @Override
    public void removeEs(Long goodsId) {
        goodsDao.deleteById(goodsId);
    }
}
